package xyz.bali16.module.nav.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.bali16.application.core.enums.ApiEnum;
import xyz.bali16.application.core.model.Result;
import xyz.bali16.application.entity.Role;
import xyz.bali16.module.nav.entity.Nav;
import xyz.bali16.module.nav.model.NavAddParam;
import xyz.bali16.module.nav.model.NavPageParam;
import xyz.bali16.module.nav.model.NavUpdateParam;
import xyz.bali16.module.nav.service.NavService;

import javax.validation.Valid;

import static xyz.bali16.application.core.model.Result.*;
import static xyz.bali16.application.core.model.Result.failure;
import static xyz.bali16.module.nav.utils.ResultUtils.errorLog;

/**
 * @author bali2 on 9/3/2023.
 * @version 1.0
 */
@RestController
@RequestMapping(BaseController.API+"nav")
@Api(tags = "导航接口")
@AllArgsConstructor

public class NavController extends BaseController{
    public static final Logger logger = LoggerFactory.getLogger(NavController.class);
    @Autowired
    NavService navService;

    @GetMapping
    @ApiOperation("分页条件查询导航列表")
    public Result<IPage<Nav>> pageQuery(@Valid NavPageParam param) {
        IPage<Nav> navIPage = navService.getPage(param);
        return success(ApiEnum.GET_SUCCESS, navIPage);
    }

    @PostMapping
    ////@PreAuthorize("@auth.permission('user:add')")
    @ApiOperation("添加导航")
    public Result<String> add(@RequestBody @Valid NavAddParam param) {
        try {
            navService.addNav(param);
            return response(ApiEnum.ADD_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "新增导航 '{}' 信息失败 {}", param.getName(), e.getMessage());
            return failure(ApiEnum.ADD_ERROR, "新增导航失败", e);
        }
    }

    @PutMapping
    ////@PreAuthorize("@auth.permission('user:update')")
    @ApiOperation("修改导航")
    public Result<String> update(@RequestBody @Valid NavUpdateParam param) {
        try {
            navService.updateNav(param);
            return response(ApiEnum.UPDATE_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "更新导航 '{}' 信息失败 {}", param.getId(), e.getMessage());
            return failure(ApiEnum.UPDATE_ERROR, "更新导航失败", e);
        }
    }

    @PutMapping("{navId}/{status}")
    ////@PreAuthorize("@auth.permission('user:update')")
    @ApiOperation("修改导航状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "navId", value = "导航id", paramType = "path", required = true),
            @ApiImplicitParam(name = "status", value = "状态（1启用, 0停用）", paramType = "path", required = true)
    })
    public Result<Role> updateStatus(@PathVariable("navId") String navId, @PathVariable("status") Integer status) {
        String message = status == 1 ? "启用" : "禁用";
        try {
            navService.updateStatus(navId, status);
            return response(ApiEnum.UPDATE_SUCCESS, message + "成功");
        } catch (Exception e) {
            errorLog(logger, e, "修改导航状态失败. {}", e.getMessage());
            return failure(ApiEnum.UPDATE_ERROR, message + "失败: ", e);
        }
    }

    @DeleteMapping("/{navId}")
    ////@PreAuthorize("@auth.permission('user:delete')")
    @ApiOperation("删除导航")
    @ApiImplicitParam(name = "navId", value = "导航id", paramType = "path", required = true)
    public Result<String> deleteNav(@PathVariable("navId") String navId) {
        try {
            navService.delete(navId);
            return response(ApiEnum.DELETE_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "删除导航 '{}' 失败. {}", navId, e.getMessage());
            return failure(ApiEnum.DELETE_ERROR, e);
        }
    }

}
